using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Plugin.ActiveForever.ActiveChat.Data;
using Nop.Plugin.ActiveForever.ActiveChat.Services;

namespace Nop.Plugin.ActiveForever.ActiveChat
{
	public class DependencyRegistrar : IDependencyRegistrar
	{
	    public int Order
		{
			get
			{
				return 0;
			}
		}

	    public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
		{
			DataSettings dataSetting = (new DataSettingsManager()).LoadSettings();
            builder.Register<IDbContext>(c => RegisterIDbContext(c, dataSetting)).Named<IDbContext>("nop_object_context_activeforever_activechat").InstancePerHttpRequest(new object[0]);
            builder.Register(c => RegisterIDbContext(c, dataSetting)).InstancePerHttpRequest(new object[0]);
            builder.RegisterType<ActiveChatService>().As<IActiveChatService>();
            builder.RegisterType<EfRepository<ClientChatLog>>().As<IRepository<ClientChatLog>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_activeforever_activechat")).InstancePerHttpRequest(new object[0]);
            builder.RegisterType<EfRepository<BannedClient>>().As<IRepository<BannedClient>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_activeforever_activechat")).InstancePerHttpRequest(new object[0]);
            builder.RegisterType<EfRepository<CannedResponse>>().As<IRepository<CannedResponse>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_activeforever_activechat")).InstancePerHttpRequest(new object[0]);
		}

		private ActiveChatObjectContext RegisterIDbContext(IComponentContext componentContext, DataSettings dataSettings)
		{
		    string str = (dataSettings == null || !dataSettings.IsValid() ? componentContext.Resolve<DataSettings>().DataConnectionString : dataSettings.DataConnectionString);
		    return new ActiveChatObjectContext(str);
		}
	}
}